using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Cashier;
using JlPay.Sdk.Models.Cashier;

namespace JlPay.Sdk.Examples.Cashier.PreOrder
{
    /// <summary>
    /// 预下单独立示例程序
    /// </summary>
    public class PreOrderDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 预下单独立示例程序 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await PreOrderDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 示例运行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfigWithCrypto();
        }

        /// <summary>
        /// 构建预下单请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static PreOrderRequest BuildPreOrderRequest()
        {
            var request = new PreOrderRequest
            {
                MerchNo = "849584358120018",
                TermNo = "60131234",
                OutTradeNo = "ld201911023465529",
                PaymentValidTime = "10",
                Description = "充值中心-会员充值",
                Attach = "商城—-易拉宝10个打印纸20箱",
                TotalAmount = "1",
                Remark = "该订单物流请走顺丰快递",
                NotifyUrl = "https://help.jlpay.com/api/docs/home",
                EnvVersion = "release",
                MerchAppid = "wx336dcaf6a1ecf632",
                ReturnUrl = "page/home/webview/webview?src=https%3A%2F%2Fmp.elong.com%2Ftenthousandaura%2F%3Factivitycode%3Dd32d7735-ae4f-440c-bb0c-dba4deba9470%26isSocket%3DHotelActivityId_8407&isRefresh=refresh&isSocket=HotelActivityId_8407**ecf632"
            };

            return request;
        }

        /// <summary>
        /// 预下单示例
        /// </summary>
        public static async Task PreOrderDemoAsync()
        {
            Console.WriteLine("开始执行预下单示例");
            
            try
            {
                // 创建配置对象和收银台服务
                var config = CreateConfig();
                var cashierService = new CashierService(config);
                
                // 创建预下单请求
                var request = BuildPreOrderRequest();
                
                // 发送请求
                var response = await cashierService.CreatePreOrderAsync(request);
                
                // 处理响应结果
                response.HandleResponse("预下单", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new PreOrderResponse();
                response.HandleResponseWithException("预下单", config, ex);
            }
            
            Console.WriteLine("预下单示例执行完毕");
        }
    }
} 